package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.query.FormalStudentChartQueryObject;
import cn.wolfcode.crm.service.IFormalStudentChartService;
import com.alibaba.fastjson.JSON;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("charts")
public class ChartsController {
    @Autowired
    private IFormalStudentChartService FormalStudentChartService;

    @RequestMapping("view")
    @RequiresPermissions(value = {"charts:view", "报表页面"}, logical = Logical.OR)
    public String view() {
        return "charts/FormalStudent";
    }

    @RequestMapping("formalStudent")
    @ResponseBody
    public List<Map<String, Object>> formalStudent(FormalStudentChartQueryObject qo) {
        return FormalStudentChartService.listFormalStudentCharts(qo);
    }

    @RequestMapping("getBar")
    @RequiresPermissions(value = {"charts:getBar", "报表柱状图"}, logical = Logical.OR)
    public String getBar(@ModelAttribute("qo") FormalStudentChartQueryObject qo, Map map) {
        List<BigDecimal> totalPaid = new ArrayList<>();
        List<BigDecimal> debt = new ArrayList<>();
        List<String> groupTypes = new ArrayList<>();
        List<Map<String, Object>> formalStudentCharts = FormalStudentChartService.listFormalStudentCharts(qo);
        for (Map<String, Object> formalStudentChart : formalStudentCharts) {
            totalPaid.add((BigDecimal) formalStudentChart.get("totalPaid"));
            debt.add((BigDecimal) formalStudentChart.get("debt"));
            Object groupType = formalStudentChart.get("groupType");
            if (groupType instanceof Integer) {
                if (groupType.equals(0)) {
                    groupTypes.add("正常");
                } else if (groupType.equals(1)) {
                    groupTypes.add("转班");
                } else if (groupType.equals(2)) {
                    groupTypes.add("流失");
                }
            } else if (groupType instanceof Long) {
                if ((Long) groupType == 32) {
                    groupTypes.add("微信");
                } else if ((Long) groupType == 33) {
                    groupTypes.add("支付宝");
                } else if ((Long) groupType == 34) {
                    groupTypes.add("网银");
                } else if ((Long) groupType == 35) {
                    groupTypes.add("借贷");
                }
            }
        }
        map.put("groupTypes", JSON.toJSONString(groupTypes));
        map.put("totalPaid", totalPaid);
        map.put("debt", debt);
        map.put("groupBy", JSON.toJSONString(FormalStudentChartQueryObject.groupTypes.get(qo.getGroupBy())));
        return "charts/FormalStudentByBar";
    }

    @RequestMapping("getPie")
    public String getPie(@ModelAttribute("qo") FormalStudentChartQueryObject qo, Map map) {
        List<String> groupTypes = new ArrayList<>();
        List<Map<String, Object>> saleCharts = FormalStudentChartService.listFormalStudentCharts(qo);
        List<Map<String, Object>> datas = new ArrayList<>();
        BigDecimal maxNumber = BigDecimal.ZERO;
        for (Map<String, Object> saleChart : saleCharts) {
            if (maxNumber.compareTo((BigDecimal) saleChart.get("totalPaid")) < 0) {
                maxNumber = (BigDecimal) saleChart.get("totalPaid");
            }
            Object groupType = saleChart.get("groupType");
            HashMap<String, Object> data = new HashMap<>();
            if (groupType instanceof Integer) {
                if (groupType.equals(0)) {
                    data.put("name", "正常");
                    groupTypes.add("正常");
                } else if (groupType.equals(1)) {
                    data.put("name", "转班");
                    groupTypes.add("转班");
                } else if (groupType.equals(2)) {
                    data.put("name","流失");
                    groupTypes.add("流失");
                }
            } else if (groupType instanceof Long) {
                if ((Long) groupType == 32) {
                    data.put("name","微信");
                    groupTypes.add("微信");
                } else if ((Long) groupType == 33) {
                    data.put("name","支付宝");
                    groupTypes.add("支付宝");
                } else if ((Long) groupType == 34) {
                    data.put("name","网银");
                    groupTypes.add("网银");
                } else if ((Long) groupType == 35) {
                    data.put("name","借贷");
                    groupTypes.add("借贷");
                }
            }
            data.put("value", saleChart.get("totalPaid"));
            datas.add(data);
        }
        map.put("groupTypes", JSON.toJSONString(groupTypes));
        map.put("datas", JSON.toJSONString(datas));
        map.put("maxNumber", maxNumber);
        map.put("groupBy", JSON.toJSONString(FormalStudentChartQueryObject.groupTypes.get(qo.getGroupBy())));
        return "charts/FormalStudentByPie";
    }
}

